
require 'rexml/document'
require 'rexml/xpath'

module Oos4ruby
  class Oos
    def auth_app(app_token, secret_key)
      @auth = Auth.new(app_token, secret_key, :app)
    end
    
    def auth_user(email, api_token)
      @auth = Auth.new(email, api_token, :wsse)
      document_service
    end
    
    def user(slug = nil)      
      User.find( @auth, slug )
    end
    
    def search(opts)
      Search.find @auth, opts
    end
   
    def privacy_cats
      return @privacy_cats if @privacy_cats
      getter = HTTPInvoker.new PRIVACY_URL, @auth
      
      worked = getter.get
      
      if worked
        @privacy_cats = {}
        cats = REXML::Document.new( getter.body ).root
        REXML::XPath.match(cats, './atom:category', XmlNamespaces).each { |cat|          
          cat.add_attribute('scheme', cats.attributes['scheme'])
          @privacy_cats[cat.attributes['term'].underscore.to_sym] = Category.new(cat)
        }
      end
      return @privacy_cats
    end

    private
    def document_service
      getter = HTTPInvoker.new API_URL, @auth
      
      worked = getter.get
      
      if worked 
        @service = Service.new(REXML::Document.new( getter.body ).root)
      end
    end
  end
end


